package szwolaiyun.paireach.com.szwolaizhuang.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;

import szwolaiyun.paireach.com.szwolaizhuang.R;
import szwolaiyun.paireach.com.szwolaizhuang.fragment.FinishWorkFragment;
import szwolaiyun.paireach.com.szwolaizhuang.fragment.HistoryFragment;
import szwolaiyun.paireach.com.szwolaizhuang.fragment.MessageHistoryFragment;
import szwolaiyun.paireach.com.szwolaizhuang.fragment.HomeFragment;
import szwolaiyun.paireach.com.szwolaizhuang.fragment.MessageInfoFragment;
import szwolaiyun.paireach.com.szwolaizhuang.fragment.PersonalCenterFragment;
import szwolaiyun.paireach.com.szwolaizhuang.fragment.ScanBarCodeFragment;
import szwolaiyun.paireach.com.szwolaizhuang.fragment.StartWorkFragment;
import szwolaiyun.paireach.com.szwolaizhuang.fragment.WorkingFragment;
import szwolaiyun.paireach.com.szwolaizhuang.mode.MessageInfo;
import szwolaiyun.paireach.com.szwolaizhuang.model.business.Operator;
import szwolaiyun.paireach.com.szwolaizhuang.util.MessageInfoUtil;

public class MainActivity extends FragmentActivity implements View.OnClickListener {

    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;
    private HomeFragment homeFragment;
    private ScanBarCodeFragment scanBarCodeFragment;
    private StartWorkFragment startWorkFragment;
    private WorkingFragment workingFragment;
    private FinishWorkFragment finishWorkFragment;
    private MessageHistoryFragment messageHistoryFragment;
    private MessageInfoFragment messageInfoFragment;
    private HistoryFragment historyFragment;

    private LinearLayout home;
    private LinearLayout message;
    private LinearLayout history;

    private Fragment showingFragment;
    private PersonalCenterFragment personalCenterFragment;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getArguments();
        setContentView(R.layout.activity_main);
        initControl();
        initFragment();
        changFragment(homeFragment);
    }

    private void getArguments() {
        Intent intent = getIntent();
    }

    void initFragment() {
        homeFragment = HomeFragment.getInstance();
        scanBarCodeFragment = new ScanBarCodeFragment();
        scanBarCodeFragment.setHandle(handler);
        startWorkFragment = new StartWorkFragment();
        startWorkFragment.setHandle(handler);
        workingFragment = new WorkingFragment();
        workingFragment.setHandle(handler);
        finishWorkFragment = new FinishWorkFragment();
        finishWorkFragment.setHandle(handler);
        messageHistoryFragment = new MessageHistoryFragment();
        messageHistoryFragment.setHandle(handler);
        messageInfoFragment = new MessageInfoFragment();
        messageInfoFragment.setHandle(handler);
        historyFragment = new HistoryFragment();
        historyFragment.setHandle(handler);
        personalCenterFragment=new PersonalCenterFragment();
    }

    void initControl() {
        fragmentManager = getSupportFragmentManager();
        home = (LinearLayout) findViewById(R.id.home);
        message = (LinearLayout) findViewById(R.id.message);
        history = (LinearLayout) findViewById(R.id.history);
        home.setOnClickListener(this);
        message.setOnClickListener(this);
        history.setOnClickListener(this);
    }

    void changFragment(Fragment fragment) {
        fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.change_layout, fragment);
        showingFragment = fragment;
        fragmentTransaction.commit();
    }


    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case MessageInfoUtil.TO_WORKING:
                    changFragment(workingFragment);
                    break;
                case MessageInfoUtil.TO_HOME:
                    changFragment(homeFragment);
                    break;
                case MessageInfoUtil.TO_MAKE_SURE_FINISH_WORK:
                    changFragment(finishWorkFragment);
                    break;
                case MessageInfoUtil.TO_MAKE_SURE_START_WORK:
                    changFragment(startWorkFragment);
                    break;
                case MessageInfoUtil.TO_SCAN:
                    if (msg.arg1 == MessageInfoUtil.TO_MAKE_SURE_START_WORK) {
                        scanBarCodeFragment.setFragmentMark(MessageInfoUtil.TO_MAKE_SURE_START_WORK);
                        changFragment(scanBarCodeFragment);
                    } else {
                        scanBarCodeFragment.setFragmentMark(MessageInfoUtil.TO_MAKE_SURE_FINISH_WORK);
                        changFragment(scanBarCodeFragment);
                    }
                    break;
                case MessageInfoUtil.TO_MESSAGE:
                    changFragment(messageInfoFragment);
                    messageInfoFragment.setMessageInfo((MessageInfo) msg.obj);
                    break;

            }
        }
    };

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && showingFragment == scanBarCodeFragment) {
            scanBarCodeFragment.goBack();
            Log.e("0011", "点击返回键，取消扫码");
            return true;
        } else {
            return super.onKeyDown(keyCode, event);
        }


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.home:
                changFragment(homeFragment);
                break;
            case R.id.message:
                changFragment(messageHistoryFragment);

                break;
            case R.id.history:
                changFragment(personalCenterFragment);
                break;
        }

    }
}
